//
//  YXBindingBankCardView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/30.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXBindingBankCardView.h"

@interface YXBindingBankCardView ()<UITextFieldDelegate>


@end

@implementation YXBindingBankCardView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        NSArray *viewArray = [[NSBundle mainBundle]loadNibNamed:@"YXBindingBankCardView" owner:self options:nil];
        self = viewArray[0];
        self.frame = frame;
        self.backgroundColor = color_backColor;
        self.smsBtn.layer.cornerRadius = 8.0f;
        self.smsBtn.layer.masksToBounds = YES;
        self.certificationBtn.layer.masksToBounds = YES;
        self.certificationBtn.layer.cornerRadius = 10.0f;
        self.certificationBtn.userInteractionEnabled = NO;
    }
    return self;
    
}

#pragma mark - 点击获取验证码
- (IBAction)smsBtnAction:(UIButton *)sender {
    
    if (self.phoneTF.text.length == 0) {
        [self showTostMessage:@"请输入手机号码"];
    }else {
//      http://121.196.210.134:8082/mpos/wx/sendBankSmsCode.do
        NSString *urlStr = @"sendBankSmsCode.do";
        NSDictionary *parameter = @{@"phone":self.phoneTF.text};
        [BX_NetWorkManager requestForPOSTWithUrl:urlStr parameter:parameter success:^(id responseObject) {
            if (REQUESTDATASUCCESS) {
                [self GCDTime];
                [self showAlertWith:@"手机短信发送成功！"];
            }
        } failure:^(NSError *error) {
            [self showAlertWithTitle:@"提示" Message:@"维护中，请稍等！"];
        }];
    }
    
}


//制作timeButton的属性文本
- (NSMutableAttributedString *)makeTimeButtonAttributed:(NSInteger)time{
    NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%li",time] attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    NSMutableAttributedString *string4 = [[NSMutableAttributedString alloc]initWithString:@"剩余" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [string4 insertAttributedString:string3 atIndex:string4.length];
    return string4;
}
#pragma mark - time
- (void)GCDTime{
    
    __block NSInteger time = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (time < 1) {
            dispatch_source_cancel(timer);
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:@"重新发送" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
            dispatch_async(dispatch_get_main_queue(), ^{
                _smsBtn.userInteractionEnabled = YES;
                [_smsBtn setAttributedTitle:string forState:(UIControlStateNormal)];
                _smsBtn.backgroundColor = color_globalColor;
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                _smsBtn.userInteractionEnabled = NO;
                _smsBtn.backgroundColor = [UIColor py_colorWithHexString:@"#CBCCCD"];
                [_smsBtn setAttributedTitle:[self makeTimeButtonAttributed:time] forState:(UIControlStateNormal)];
                _certificationBtn.backgroundColor = color_globalColor;
                _certificationBtn.userInteractionEnabled = YES;

            });
            time --;
        }
    });
    dispatch_resume(timer);
}

#pragma mark - 点击完成认证
- (IBAction)certificationBtnAction:(UIButton *)sender {
    
    if (self.cardNumberTF.text.length == 0 || self.phoneTF.text.length == 0 || self.smsTF.text.length == 0 || self.passwordTF.text.length == 0 || self.password2TF.text.length == 0) {
        [self showTostMessage:@"请填写完整信息！"];
    }else if (self.passwordTF.text != self.password2TF.text) {
        [self showTostMessage:@"提现密码不相同！"];
    }else {
        NSDictionary *dataDic = @{@"bankNo":self.cardNumberTF.text,@"phone":self.phoneTF.text,@"vCode":self.smsTF.text,@"wPassword":self.passwordTF.text,@"cPassword":self.password2TF.text};
        if (self.clickCompleteBtnBlock) {
            self.clickCompleteBtnBlock(dataDic);
        }
    }
   
}



@end
